package com.seven.controller;

import com.seven.commons.BaseResponse;
import com.seven.commons.BaseResultEnum;
import com.seven.commons.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@RestController
@RequestMapping("${server.error.path:${error.path:/error}}")
public class ErrController implements ErrorController {

    private static final String PATH = "/error";

    @Override
    public String getErrorPath() {
        return PATH;
    }

    /**
     * 进行页面调用接口没有显示的问题的展示,view的显示
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(produces = "text/html")
    public String errorHtml(HttpServletRequest request, HttpServletResponse response) {

        BaseResultEnum resultEnum = getResultEnum(response.getStatus());

        BaseResponse resp = BaseResponse.buildFail(resultEnum.getCode(), resultEnum.getMsg());

        return JsonUtils.objectToJson(resp);

    }

    /**
     * 进行json的请求rest的接口测试的调用地址,定义枚举异常类的处理，处理HTTP相关的错误
     * @param request
     * @return
     */
    @RequestMapping
    public BaseResponse error(HttpServletRequest request,HttpServletResponse response) {

        BaseResultEnum resultEnum = getResultEnum(response.getStatus());

        return BaseResponse.buildFail(resultEnum.getCode(),resultEnum.getMsg());

    }

    /**
     * @descrp:根据http response的状态信息封装返回的文案
     * @auther: sevenDay
     * @date: 2018/12/20 13:01
     * @param status :
     * @return : com.seven.commons.BaseResultEnum
     */

    private BaseResultEnum getResultEnum(final int status){
        BaseResultEnum resultEnum =null;
        switch (status){
            case HttpServletResponse.SC_BAD_REQUEST:
                resultEnum = BaseResultEnum.HTTP_REQUEST_BAD_REQUEST;
                break;
            case HttpServletResponse.SC_UNAUTHORIZED:
                resultEnum =BaseResultEnum.HTTP_REQUEST_UNAUTHORIZED;
                break;
            case HttpServletResponse.SC_FORBIDDEN:
                resultEnum = BaseResultEnum.HTTP_REQUEST_FORBIDDEN;
                break;
            case HttpServletResponse.SC_NOT_FOUND:
                resultEnum = BaseResultEnum.HTTP_REQUEST_NOT_FOUND;
                break;
            case HttpServletResponse.SC_METHOD_NOT_ALLOWED:
                resultEnum = BaseResultEnum.HTTP_REQUEST_METHOD_NOT_ALLOWED;
                break;
            case HttpServletResponse.SC_NOT_ACCEPTABLE:
                resultEnum = BaseResultEnum.HTTP_REQUEST_NOT_ACCEPTABLE;
                break;
            case HttpServletResponse.SC_LENGTH_REQUIRED:
                resultEnum =BaseResultEnum.HTTP_REQUEST_LENGTH_REQUIRED;
                break;
            case HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE:
                resultEnum = BaseResultEnum.HTTP_REQUEST_UNSUPPORTED_MEDIA_TYPE;
                break;
            case HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE:
                resultEnum = BaseResultEnum.HTTP_REQUEST_REQUESTED_RANGE_NOT_SATISFIABLE;
                break;
            case HttpServletResponse.SC_SERVICE_UNAVAILABLE:
                resultEnum = BaseResultEnum.HTTP_REQUEST_SERVICE_UNAVAILABLE;
                break;
            default:
                resultEnum = BaseResultEnum.UNKNOWN_ERROR;

        }
        return  resultEnum;

    }

}
